package com.jzrh.flashbee.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzrh.flashbee.common.core.exception.ParamException;
import com.jzrh.flashbee.common.utils.AccessKeyGenerator;
import com.jzrh.flashbee.entity.FlashbeeUser;
import com.jzrh.flashbee.entity.dto.LoginDTO;
import com.jzrh.flashbee.mapper.FlashbeeUserMapper;
import com.jzrh.flashbee.service.IFlashbeeUserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Jince
 * @since: 2023.11.22 下午 11:25
 * @description:
 */
@Service
public class FlashbeeUserServiceImpl extends ServiceImpl<FlashbeeUserMapper, FlashbeeUser> implements IFlashbeeUserService {
    @Value("${password}")
    private String password;

    /**
     * 登录
     * @param loginDTO 用户名
     * @param request 请求头
     * @return true，false
     */
    @Override
    public Boolean login(LoginDTO loginDTO, HttpServletRequest request) {
        if (password.equals(loginDTO.getPassword())) {
            HttpSession session = request.getSession();
            session.setAttribute(request.getRemoteAddr(), LocalDateTime.now());
            session.setMaxInactiveInterval(24 * 60 * 60);
            return true;
        }
        return false;
    }

    /**
     * 密钥是否存在
     *
     * @param accessKey key
     * @param username  username
     * @return true存在，false不存在
     */
    @Override
    public Boolean existAccessKey(String accessKey, String username) {
        LambdaQueryWrapper<FlashbeeUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FlashbeeUser::getAccessKey, accessKey).eq(FlashbeeUser::getUsername, username);
        return this.count(lambdaQueryWrapper) > 0;
    }

    /**
     * 生成密钥
     *
     * @param username 用户名
     * @return 密钥
     */
    public Map<String, String> generateAccessKey(String username) {
        if (StringUtils.isBlank(username) || username.length() < 8) {
            throw new ParamException("用户名不能为空并且长度必须大于8位数");
        }
        LambdaQueryWrapper<FlashbeeUser> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(FlashbeeUser::getUsername, username);
        if (!(this.count(lambdaQueryWrapper) > 0)) {
            String accessKey = AccessKeyGenerator.generateAccessKey();
            FlashbeeUser flashbeeUser = new FlashbeeUser();
            flashbeeUser.setAccessKey(accessKey);
            flashbeeUser.setUsername(username);
            this.save(flashbeeUser);
            Map<String, String> map = new HashMap<>();
            map.put("AccessKey", accessKey);
            return map;
        }
        throw new ParamException("用户名以生成过密钥了");
    }
}
